package com.example.demo.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

    public static String toLowerCaseFirstOne(String str) {
        if (isEmpty(str)) {
            return str;
        }
        String firstStr = str.substring(0, 1);
        return firstStr.toLowerCase() + str.substring(1);
    }

    /**
     * 隐藏手机字符串部分字段
     *
     * @param mobileStr
     * @return
     */
    public static String mobileFilter(String mobileStr) {
        return mobileStr.substring(0, 3) + "****" + mobileStr.substring(7, 11);
    }

    /**
     * 隐藏邮箱字符串部分字段
     *
     * @param mailStr
     * @return
     */
    public static String mailFilter(String mailStr) {
        String[] email = mailStr.split("@");
        String tempStr = email[0].substring(0, 3);
        for (int i = 0; i < email[0].length() - 3; i++) {
            tempStr += "*";
        }
        tempStr += "@";
        tempStr += email[1];
        return tempStr;
    }

    /**
     * 隐藏银行卡号字符串部分字段,只显示前6后4位
     *
     * @param cardNoStr
     * @return
     */
    public static String cardNoFilter(String cardNoStr) {
        StringBuffer sb = new StringBuffer();
        int length = cardNoStr.length();
        for (int i = 0; i < length; i++) {
            if (i < 6 || (i > length - 5)) {
                sb.append(cardNoStr.charAt(i));
            } else {
                sb.append("*");
            }
        }
        return sb.toString();

    }

    /**
     * BigDecimal格式的金额 转 BCD格式的金额  示例：1.11 转 000000000111
     *
     * @param bigDecimalAmt
     * @return
     */
    public static String amtToBCD(BigDecimal bigDecimalAmt) {
        bigDecimalAmt.setScale(2, RoundingMode.HALF_UP);
        bigDecimalAmt = bigDecimalAmt.abs().multiply(new BigDecimal("100"));
        return StringUtils.leftPad(bigDecimalAmt.intValue() + "", 12, "0");
    }

    /**
     * BCD格式的金额 转 BigDecimal格式的金额  示例：000000000111 转 1.11
     *
     * @param bcdAmt
     * @return
     */
    public static BigDecimal amtToBigDecimal(String bcdAmt) {
        BigDecimal bigDecimal = new BigDecimal(bcdAmt);
        bigDecimal.setScale(2, RoundingMode.HALF_UP);
        bigDecimal = bigDecimal.divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
        return bigDecimal;
    }

    public static boolean isAmountFormat(String amount) {
        boolean isAmountFormat = false;
        if (amount.contains(".")) {
            String[] amounts = amount.split("\\.");
            if (amounts[0].length() <= 8 && amounts[0].length() > 0) {
                isAmountFormat = true;
            }
        }
        return isAmountFormat;
    }

    /**
     * 字节级切割,确保切割出的字符对应的字节数小等于传入参数。<p>
     * 且字符完整
     *
     * @param input   输入字符串
     * @param charset 字符集
     * @param max     最大字节数
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String spiltByByteMaxLength(String input, String charset, int max) throws UnsupportedEncodingException {
        if (input == null)
            return null;
        int offset = 0;
        StringBuilder sb = new StringBuilder(max);
        for (int i = 0; i < input.length(); i++) {
            offset += input.substring(i, i + 1).getBytes(charset).length;
            if (offset > max)
                break;

            sb.append(input.charAt(i));
        }
        return sb.toString();
    }

    public static String toRemark(String input) {
        try {
            return spiltByByteMaxLength(input, "utf-8", 210);
        } catch (Exception e) {
            return input.substring(0, 210 / 3);
        }
    }


    public static boolean isDate(String dateStr) {
        if (DateUtils.stringToDate(dateStr, "yyyy-MM-dd") == null) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isMobile(String mobile) {
        if (isBlank(mobile)) return false;
        String tmp = mobile.trim();
        if (tmp.length() < 7) {
            return false;
        }
        if (tmp.substring(0,1).equalsIgnoreCase("+")) {
            tmp = mobile.substring(1, mobile.length() - 1);
        }
        return isNumeric(tmp);
    }

}
